﻿Imports 地图商家采集系统

Public Class CMap360
    Inherits CMap
    Implements IMap
    Public Sub New(mapTask As Model_Task, callback As Action(Of CProductInfo))
        task = mapTask
        Me.UpdateView = callback
    End Sub
#Region "360采集方法"
    Public Sub Deal() Implements IMap.Deal
        Log($"{MapMode.S360.GetDescription}信息解析中...")
        Dim areas = task.Areas
        For Each area In areas
            Dim code = GetCode(area.City)
            GetProducts(code, area.City)
            ''通过城市码得到HTML,并获取列表页相关信息类BaiDuListInfo
            'GetBaiduProducts(code, area.City)
        Next
    End Sub
    Private Sub GetProducts(code As String, city As String)
        Dim keywords = task.keyWords
        For Each k As CKeyword In keywords
            Dim FlagPageEnd As Boolean = False, Page = 0
            Dim infoCount As Integer = 0
            Dim prePage As String = ""
            Dim success As Integer
            Do
                success = 0
                Page += 1
                Dim cityName = Uri.EscapeDataString(city)
                Dim kw As String = Uri.EscapeDataString(k.Keyword)
                'https://ditu.so.com/app/pit?jsoncallback=jQuery18307163703623674016_1543284501939&keyword=美食&cityname=镇江&city=0511&cityid=321100&batch=2,3,4,5,6&number=10&citysuggestion=true&qii=true&region_id=&map_cbc=on&scheme=https&ext=&regionType=rectangle&sid=1000&mobile=1&from_city_card=0
                '20181127
                Dim url = $"https://ditu.so.com/app/pit?jsoncallback=jQuery18307163703623674016_1543284501939&keyword={kw}&cityname={cityName}&cityid={code}&batch={Page}"
                'Dim url = $"https://restapi.map.so.com/newapi?keyword={kw}&cityname={cityName}&cityid={code}&batch={Page}"
                Dim count As Integer = 1
                Dim html As String = ""
                Dim exp As Exception
                Do
                    exp = Nothing
                    html = Sy.Text.Encode.RemoveEscape_HTML(
                        Sy.Text.Encode.RemoveEscape_Java(Sy.Net.Factory.Http(url).SetExceptionHandle(Function() exp).Get())) 'GetHtml(url, Exception:=exp)
                    count += 1
                Loop While exp IsNot Nothing And count < 5

                If exp Is Nothing Then
                    '20181127
                    Dim arrSpliter = "primaryid", arrSpliter2 = """name"":"""
                    If html.Length < 0 OrElse html = prePage OrElse Not html.Contains(arrSpliter) Then
#Region "采集列表页信息"
                        Log("关键字：" & k.Keyword & "未采集到信息,请确认")
                        FlagPageEnd = True
                    Else
                        prePage = html
                        Dim items As String()
                        If html.Contains(arrSpliter2) Then
                            items = html.Split({arrSpliter2}, StringSplitOptions.RemoveEmptyEntries)
                        ElseIf html.Contains(arrSpliter) Then
                            items = html.Split({arrSpliter}, StringSplitOptions.RemoveEmptyEntries)
                        Else
                            items = Nothing
                        End If
                        If Not items.Count > 0 Then
                            Log("关键字：" & k.Keyword & "未采集到信息,请确认")
                            FlagPageEnd = True
                        Else
                            For i = 1 To items.Count - 1
                                Dim item As Sy.String = items(i)
                                Dim address As String = ""
                                Dim area As String = ""
                                Dim workplane As String = ""
                                Dim title As String = ""
                                Dim phone As String = ""
                                Dim mobile As String = ""
                                Dim x As String = 0
                                Dim y As String = 0
#Region "地址"
                                Dim areaSpliter = """area"":"""
                                If item.Contains(areaSpliter) Then area = Split(item, areaSpliter)(1).Split({""""}, StringSplitOptions.None)(0)
                                Dim addrSpliter = """address"":"""
                                If item.Contains(addrSpliter) Then address = Split(item, addrSpliter)(1).Split({""""}, StringSplitOptions.None)(0)
#End Region
#Region "坐标"
                                Dim xSpliter = """x"":"
                                Dim ySpliter = """y"":"
                                If item.Contains(xSpliter) Then x = Split(item, xSpliter)(1).Split({",", "}"}, StringSplitOptions.None)(0)
                                If item.Contains(ySpliter) Then y = Split(item, ySpliter)(1).Split({",", "}"}, StringSplitOptions.None)(0)
                                workplane = "X:" & x & "|Y:" & y
#End Region
#Region "标题"
                                Dim titleSpliter = "poi_name"":""", titleSpliter2 As String = """name"":"""

                                If item.Contains(titleSpliter) Then
                                    title = Split(Split(item, titleSpliter)(1), """")(0)
                                ElseIf html.Contains(titleSpliter2) Then
                                    title = Split(item, """")(0)
                                Else
                                    title = ""
                                End If
#End Region
#Region "手机号"
                                Dim phoneSpliter = """phone"":"""
                                If item.Contains(phoneSpliter) Then mobile = Split(item, phoneSpliter)(1).Split({""""}, StringSplitOptions.None)(0).Trim
                                '座机号
                                Dim telSpliter = """tel"":"""
                                If item.Contains(telSpliter) Then phone = Split(item, telSpliter)(1).Split({""""}, StringSplitOptions.None)(0).Trim
#End Region
#Region "访问详情页所需ID(备用，目前不需要,使用时，关闭注释即可)"
#End Region
#Region "采集过滤,添加"

                                If FilterResult(title, phone, mobile, address) AndAlso FilterKeywordResult(title, task.FilterKeywords) Then
                                    Dim info = New CProductInfo(address, title, workplane, city, k.Keyword, MapMode.S360.GetDescription, url, phone, mobile, area)
                                    UpdateView(info)
                                    success += If(info.Add(), 1, 0)
                                End If
                            Next
                        End If
                        If items.Count <= 1 Then FlagPageEnd = True
                        If success = 0 Then FlagPageEnd = True
#End Region
                    End If
                Else
                    Log("采集超时异常")
                    FlagPageEnd = True
                End If
            Loop Until FlagPageEnd
        Next
    End Sub
#End Region
#End Region

End Class
